package edu.gatech.cs4400.spring2014.A8.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.gatech.cs4400.spring2014.A8.control.Session;
import edu.gatech.cs4400.spring2014.A8.control.WaitedOn;

/**
 * Panel with all the option buttons from the main menu
 * Displays different buttons based upon the usertype of the current session
 * 
 * @author Stephen
 */
public class MainMenuPanel extends JPanel implements WaitedOn, ActionListener {
	//Enum of feedback signals from button presses
	public static enum Feedback{LOGOUT, MESSAGES, 
								P_EDIT_PROFILE, P_FIND_APPOINTMENTS, P_EDIT_PAYMENT_INFO, P_VISIT_HISTORY, P_RATE, 
								D_EDIT_PROFILE, D_VIEW_APPOINTMENTS, D_PATIENT_HISTORY, D_RECORD_VISIT, D_RECORD_SURGERY};
	
	private static final long serialVersionUID = 1L;
	
	private HashMap<JButton, Feedback> button2feedback;
	private boolean finished;
	private Feedback feedback;
	
	private JPanel innerPanel;

	public MainMenuPanel() {
		finished = false;
		
		//map buttons to feedback signals
		button2feedback = new HashMap<JButton, Feedback>();
		
		//logout button should be at the top of the menu, no matter the user type
		JPanel topPanel = new JPanel();
		JButton logoutButton = new JButton("Logout");
		button2feedback.put(logoutButton, Feedback.LOGOUT);
		topPanel.add(new JLabel(String.format("Logged in as %s", Session.getUsername())));
		topPanel.add(logoutButton);
		
		innerPanel = new JPanel();
		innerPanel.setLayout(new BorderLayout());
		innerPanel.add(topPanel, BorderLayout.NORTH);
		
		logoutButton.addActionListener(this);
		
		//Create custom inner panel based on user type
		if (Session.getAccountType() == Session.AccountType.PATIENT) {
			innerPanel.add(new PatientMainPanel(), BorderLayout.CENTER);
		}
		else if (Session.getAccountType() == Session.AccountType.DOCTOR) {
			innerPanel.add(new DoctorMainPanel(), BorderLayout.CENTER);
		}
		else if (Session.getAccountType() == Session.AccountType.AA) {
			innerPanel.add(new AAMainPanel(), BorderLayout.CENTER);
		}
	
		this.add(innerPanel);
	}

	/**
	 * Used when controller is waiting on this panel for user input
	 */
	@Override
	public boolean isDone() {
		return finished;
	}
	
	/**
	 * Used to reset the user feedback flags
	 */
	public void reset() {
		feedback = null;
		finished = false;
	}
	
	/**
	 * Give controller signal indicating the user input
	 * @return
	 */
	public Feedback getFeedback() {
		return feedback;
	}

	/**
	 * When user input is received, set the feedback signal and wake the
	 *  controller
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		feedback = button2feedback.get(arg0.getSource());
		finished = true;
		synchronized(this) {
			this.notify();
		}
	}
	
	/**
	 * Custom sub-panel for the patient's main menu
	 * 
	 * @author Stephen
	 */
	private class PatientMainPanel extends JPanel {
		private static final long serialVersionUID = 1L;
		
		public PatientMainPanel() {
			JButton editProfileButton = new JButton("Edit Profile");			
			button2feedback.put(editProfileButton, Feedback.P_EDIT_PROFILE);
			editProfileButton.addActionListener(MainMenuPanel.this);
			
			JButton findAppointmentsButton = new JButton("Find Appointments");
			button2feedback.put(findAppointmentsButton, Feedback.P_FIND_APPOINTMENTS);
			findAppointmentsButton.addActionListener(MainMenuPanel.this);
			
			JButton paymentEditButton = new JButton("Edit Payment Options");
			button2feedback.put(paymentEditButton, Feedback.P_EDIT_PAYMENT_INFO);
			paymentEditButton.addActionListener(MainMenuPanel.this);
			
			JButton viewVisitHistoryButton = new JButton("View Visit History");
			button2feedback.put(viewVisitHistoryButton, Feedback.P_VISIT_HISTORY);
			viewVisitHistoryButton.addActionListener(MainMenuPanel.this);
			
			JButton rateDoctorButton = new JButton("Rate Doctors");
			button2feedback.put(rateDoctorButton, Feedback.P_RATE);
			rateDoctorButton.addActionListener(MainMenuPanel.this);
			
			JButton messagesButton = new JButton("View and Send Messages");
			button2feedback.put(messagesButton, Feedback.MESSAGES);
			messagesButton.addActionListener(MainMenuPanel.this);
			
			this.setLayout(new GridLayout(6,1));
			this.add(editProfileButton);
			this.add(findAppointmentsButton);
			this.add(paymentEditButton);
			this.add(viewVisitHistoryButton);
			this.add(rateDoctorButton);
			this.add(messagesButton);
		}
	}

	/**
	 * Custom sub-panel for the doctor's main menu
	 * 
	 * @author Stephen
	 */
	private class DoctorMainPanel extends JPanel {
		private static final long serialVersionUID = 1L;
		
		public DoctorMainPanel() {
			JButton editProfileButton = new JButton("Edit Profile");
			button2feedback.put(editProfileButton, Feedback.D_EDIT_PROFILE);
			editProfileButton.addActionListener(MainMenuPanel.this);
			
			JButton viewAppointmentsButton = new JButton("View Appointments");
			button2feedback.put(viewAppointmentsButton, Feedback.D_VIEW_APPOINTMENTS);
			viewAppointmentsButton.addActionListener(MainMenuPanel.this);
			
			JButton viewPatientHistoryButton = new JButton("View Patient History");
			button2feedback.put(viewPatientHistoryButton, Feedback.D_PATIENT_HISTORY);
			viewPatientHistoryButton.addActionListener(MainMenuPanel.this);
			
			JButton recordVisitButton = new JButton("Record Visit");
			button2feedback.put(recordVisitButton, Feedback.D_RECORD_VISIT);
			recordVisitButton.addActionListener(MainMenuPanel.this);
			
			JButton recordSurgeryButton = new JButton("Record Surgery");
			button2feedback.put(recordSurgeryButton, Feedback.D_RECORD_SURGERY);
			recordSurgeryButton.addActionListener(MainMenuPanel.this);
			
			JButton messagesButton = new JButton("View and Send Messages");
			button2feedback.put(messagesButton, Feedback.MESSAGES);
			messagesButton.addActionListener(MainMenuPanel.this);
			
			this.setLayout(new GridLayout(6,1));
			this.add(editProfileButton);
			this.add(viewAppointmentsButton);
			this.add(viewPatientHistoryButton);
			this.add(recordVisitButton);
			this.add(recordSurgeryButton);
			this.add(messagesButton);
		}
	}
	
	/**
	 * Custom sub-panel for the administrative assistant's main menu
	 * 
	 * @author Stephen
	 */
	private class AAMainPanel extends JPanel {
		private static final long serialVersionUID = 1L;
		public AAMainPanel() {
			this.add(new JLabel("Administrative assistants have no database privileges"));
		}
		
	}
}
